package com.xunyin.modules.mtstore.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.api.XunyinException;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.mtstore.domain.MtStoreRoom;
import com.xunyin.modules.mtstore.service.MtStoreRoomService;
import com.xunyin.modules.mtstore.service.mapper.MtStoreRoomMapper;
import com.xunyin.modules.shareorder.service.dto.ShareCountDto;
import com.xunyin.modules.storeroom.param.TStoreRoomQueryParam;
import com.xunyin.modules.storeroom.service.dto.TStoreRoomQueryCriteria;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 包间管理业务实现类
 * <p>
 * Created by xunyinyunshang <3594419785@qq.com>
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "sysXbuser")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MtStoreRoomServiceImpl extends BaseServiceImpl<MtStoreRoomMapper, MtStoreRoom> implements MtStoreRoomService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private MtStoreRoomMapper mtStoreRoomMapper;

    /**
     * 根据id获取茶厅（包间）信息
     *
     * @return jsonObject
     */
/*    @Override
    public TStoreRoomVo getInfoTeaChild(Integer id) {
        return tStoreRoomMapper.getInfoTeaChild(id);
    }*/
    @Override
    public Map<String, Object> queryAll(TStoreRoomQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<MtStoreRoom> page = new PageInfo<>(queryAll(criteria));
        //List<TStoreRoomDto> tStoreRoomDto = new ArrayList<>();
        Map<String, Object> map = new LinkedHashMap<>(2);
//        List<MtStoreRoom> list = page.getList();
//        if (!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(criteria.getStoreIdList())) {
//            list = list.stream().filter(item -> criteria.getStoreIdList().contains(item.getStoreId()))
//        }
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<MtStoreRoom> queryAll(TStoreRoomQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(MtStoreRoom.class, criteria));
    }

    /**
     * 获取房间列表
     *
     * @return List<TStoreRoomVo>
     */
    @Override
    public List<MtStoreRoom> listRoom(TStoreRoomQueryParam queryParam) {
        LambdaQueryWrapper<MtStoreRoom> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MtStoreRoom::getStoreId, queryParam.getStoreId());
        List<MtStoreRoom> list = generator.convert(baseMapper.selectList(wrapper), MtStoreRoom.class);
        return list;
    }

    @Override
    public List<ShareCountDto> findRoomName(Integer storeId) {
        return mtStoreRoomMapper.findRoomName(storeId);
    }

    /**
     * 根据店铺ID获取房间信息
     *
     * @param id 房间ID
     */
    @Override
    public MtStoreRoom queryStoreRoomById(Integer id) {
        if (id == null || id < 1) {
            return null;
        }
        return mtStoreRoomMapper.selectById(id);
    }

    /**
     * 更新房间上线状态，A：有效/启用；D：无效
     *
     * @param id       房间ID
     * @param roomOnlineStatus   上线状态
     */
    @Override
    @Transactional
    //@OperationServiceLog(description = "更新房间上线状态")
    public void updateRoomOnlineStatus(Integer id,  String roomOnlineStatus) {
        MtStoreRoom mtStoreRoom = this.queryStoreRoomById(id);
        if (null == mtStoreRoom) {
            //throw new BusinessCheckException("该店铺不存在.");
            throw new XunyinException("该房间不存在.");
        }
        mtStoreRoom.setRoomOnlineStatus(roomOnlineStatus);
        mtStoreRoom.setUpdateTime(new Date());
        mtStoreRoomMapper.updateById(mtStoreRoom);
    }

/*    @Override
    public List<ShareCountDto> findRoomName() {
        return tStoreRoomMapper.findRoomName();
    }*/
}
